<template>

  <div>
    <div style="margin: 16px 0 0 30px ">
      <div style="width: 150px;float: left;">
        <el-input
        placeholder="请输入内容"
        v-model="input"
        clearable>
      </el-input>
      </div>
      <el-button type="primary" @click="search(input)">搜索</el-button>
      <el-button type="success" plain @click="refresh">刷新</el-button>
      <el-button  type="success" round @click="dialogFormVisiblePost = true" >添加</el-button>
    </div>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >

      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.categoryId }}
        </template>
      </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="scope">
          <!--测试数据-->
<!--          {{ scope.row }}-->
          {{ scope.row.categoryName }}
        </template>
      </el-table-column>

      <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
<!--          <el-button type="success" round @click="dialogFormVisiblePost = true">添加</el-button>-->
          <el-button type="primary" round @click="dialogFormVisiblePutId(scope.$index)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="categoryDel(scope.row.categoryId)"> </el-button>
        </template>
      </el-table-column>
    </el-table>




    <!--添加类型-->
    <el-dialog title="添加类型"  :visible.sync="dialogFormVisiblePost">
      <el-form :model="listPost">
        <el-form-item>
          <h2>请输入新增的类型 ： </h2>
        </el-form-item>
        <el-form-item label="新增类型: " :label-width="formLabelWidth">
          <el-input v-model="listPost.categoryName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePost = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePostGo(listPost.categoryName)">确 定</el-button>
      </div>
    </el-dialog>


    <!--修改类型-->
    <el-dialog title="修改类型"  :visible.sync="dialogFormVisiblePut">
      <el-form :model="listPut">
        <el-form-item>
          <h2>修改的类型为 ： {{listPut.categoryId}}</h2>
        </el-form-item>
        <el-form-item label="类型: " :label-width="formLabelWidth">
          <el-input v-model="listPut.categoryName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePut = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePutGo(listPut)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
    </div>
</template>

<script>
// import { getList } from '@/api/table'
import { categoryList,categoryDelApi,dialogFormVisiblePutApi,dialogFormVisiblePostApi,searchApi } from '@/api/server-category'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      input:null,
      list: null,
      listPut: [ // 修改类型的数据
        {
          "categoryId": null,
          "categoryName": null,
          "types": null,
          "goods": null
        },
      ],
      listPost: [ // 添加类型的数据
        {
          "categoryId": null,
          "categoryName": null,
          "types": null,
          "goods": null
        },
      ],
      listLoading: true,
      dialogFormVisiblePut: false, // 修改类型弹窗
      dialogFormVisiblePost: false, // 添加类型弹窗
      form: {
        name: '12313',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {

    //搜索
    search(input){
      var that = this;
      searchApi(input,function (msg) {
        that.list = msg.data

      })

    },


    /*根据id添加类型*/
    dialogFormVisiblePostGo(categoryName){
      var that = this;
      dialogFormVisiblePostApi(categoryName,function(msg){
        if(msg.msg == '添加成功'){
          that.dialogFormVisiblePost = false;
          alert(msg.msg);
          that.fetchData();
        }
      })
    },



    /*根据id修改类型*/
    dialogFormVisiblePutGo(listPut){
      var that = this;
      dialogFormVisiblePutApi(listPut,function (msg) {
        if(msg.msg == '修改成功'){
          that.dialogFormVisiblePut = false;
          alert(msg.msg);
        }
      })
    },
    /*修改获取当前数据*/
    dialogFormVisiblePutId(index){
      var that = this;
      that.listPut = that.list[index];
      that.dialogFormVisiblePut = true;
     },


    // 根据id删除类型
    categoryDel(categoryId) {
      var that = this;
      categoryDelApi(categoryId, function(msg) {
        alert(msg.data.msg)
        if(msg.data.msg == '删除成功'){
          that.fetchData()
        }

      })
    },

    //手动刷新
    refresh(){
      var that = this;
      that.fetchData()
    },

    // 自动加载刷新
    fetchData() {
      var that = this
      that.listLoading = true
      categoryList(function(msg) {
        console.log(msg)
        that.list = msg.data
        if (msg.msg === '查询成功') {
          that.listLoading = false
        }
      })

      // this.listLoading = true
      // getList().then(response => {
      //   this.list = response.data.items
      //   this.listLoading = false
      // })
    }
  }
}
</script>
